package org.example.session.defaults;

import org.example.binding.MapperRegistry;
import org.example.executor.Executor;
import org.example.mapping.BoundSql;
import org.example.mapping.Environment;
import org.example.mapping.MappedStatement;
import org.example.session.Configuration;
import org.example.session.SqlSession;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author: luyg
 * @Date: 2024/11/10/4:03
 * @Description:
 */
public class DefaultSqlSession implements SqlSession {
    private Configuration configuration;
    private Executor executor;
    public DefaultSqlSession(Configuration configuration,Executor executor) {
        this.configuration = configuration;
        this.executor=executor;
    }

    @Override
    public <T> T selectOne(String statement) {
        return this.selectOne(statement, null);

    }

    @Override
    public <T> T selectOne(String statement, Object parameter) {
        MappedStatement mappedStatement = configuration.getMappedStatement(statement);
        List<T> query = executor.query(mappedStatement, parameter, Executor.NO_RESULT_HANDLER, mappedStatement.getBoundSql());
        return query.get(0);
    }
    @Override
    public <T> T getMapper(Class<T> type) {
        return configuration.getMapper(type, this);
    }

    @Override
    public Configuration getConfiguration() {
        return configuration;
    }


}
